/*
   Copyright (c) 2010, The AgreGo Team <team@agrego.net>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE

 This project has begun as an "Epitech Innovative Project" in Epitech, France.
*/
#ifndef _HANDSHAKE_H_
#define _HANDSHAKE_H_

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>
#include <unistd.h>
#include <signal.h>

/**
 * used to generate the publicKey
 */
#include <time.h>

/**
 * used to generate the hash string
 */
#include <openssl/sha.h>

#include "xlib.h"
#include "connexion.h"

/**
 * This is number of waves
 */
#define WAVES_NUMBER 3

/**
 * the public key length with the null terminate unsigned character
 */
#define PUBKEY_LENGTH	26

/**
 * the public key format, us in strftime
 */
#define PUBKEY_FORMAT	"!';%Y-%m-%d$%H:%M)R2[&(!"
/**
 * the message send to accept the connexion
 */
#define HANDSHAKE_MSG_OK "MirabLapLeglantine"

/**
 * contains functions to handle handshake
 */

/**
 * run the handshake
 * this function will be different for the server and the client
 * @param the connexion to open
 * @return 0 if the handshake doesn't ok, 1 else
 * @author Ligro
 */
void	doHandshake(struct Connexion *);

/**
 * Enables a connexion
 * @param	The connexion to open
 * @param	A pipefd array
 */
void	enableConnAfterHandshake(int *pipefd);

/**
 * generate a public key
 * @param a unsigned char * previously malloced with SHA_DIGEST_LENGTH * sizeof(unsigned char)
 * @author Ligro
 */
void	genPublicKey(unsigned char *);

/**
 * get the hash using the public and private key
 * @param a public key generated by genPublicKey()
 * @param a unsigned char * previously malloced with SHA_DIGEST_LENGTH * sizeof(unsigned char)
 * @author Ligro
 */
void	getHash(const unsigned char *, unsigned char *);

/**
 * check a hash with a public key
 * @param a public key
 * @param a hash related to the public key
 * @author Ligro
 */
int	checkHash(const unsigned char *, const unsigned char *);

/**
 * run the handshake
 * this function will be different for the server and the client
 * it should be run in a son process
 * @param the connexion to open
 * @param process id
 * @return 0 if the handshake doesn't ok, 1 else
 * @author Ligro
 */
int	checkMyHand(struct Connexion *);

/**
 * do a select with a timeout
 * @param the socket which wait for a reading
 * @author Ligro
 */
int	waitForRead(int );

/**
 * check the ip source ??? here ???
 *//*
int	checkIp(struct sockaddr *);
    */

#endif /* _HANDSHAKE_H_ */
